package com.hrt.datasender.datadao.model;

import java.util.List;

import com.btcode.exception.BusinessException;
import com.btcode.orm.model.IDomainField;
import com.btcode.orm.model.IField;
import com.btcode.orm.model.IModelAddition;
import com.btcode.orm.relation.ex.IRelation;
import com.hrt.datasender.datadao.service.BcSendPlatformService;

public class BcSendPlatformModelAddition implements IModelAddition<BcSendPlatformModel> {

    @Override
    public void generateDefaultValue(BcSendPlatformModel model) {
        // TODO Auto-generated method stub

    }

    @Override
    public void initField(List<IField> fields) {
        // TODO Auto-generated method stub

    }

    @Override
    public void initRelations(List<IRelation> relations) {
        // TODO Auto-generated method stub

    }

    @Override
    public void initDomainField(List<IDomainField> domainFields) {
        // TODO Auto-generated method stub

    }

    /**
     * 
     * <pre>
     * 注释
     * </pre>
     * 
     * @author FAN
     * @date 2018年9月12日下午5:23:41
     * @param model
     */
    @Override
    public void beforeCreate(BcSendPlatformModel model) {

        List<BcSendPlatformModel> list = BcSendPlatformService.getInstance().getModelList();

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) != null) {
                if (list.get(i).getName().equals(model.getName())) {
                    throw new BusinessException("发送平台名称不能重复");
                }
                if (list.get(i).getMn().equals(model.getMn()) && list.get(i).getIp().equals(model.getIp())) {
                    throw new BusinessException("发送平台MN和IP不能同时重复");
                }
            }
        }

    }

    @Override
    public void afterCreate(BcSendPlatformModel model) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeUpdate(BcSendPlatformModel model) {
        List<BcSendPlatformModel> list = BcSendPlatformService.getInstance().getModelList();

        for (int i = 0; i < list.size(); i++) {
            if (!(model.getPlatformId()).equals(list.get(i).getPlatformId())){
                if (model.getName().equals(list.get(i).getName())) {

                    throw new BusinessException("站点名称不能重复");
                }
            }
        }
    }

    @Override
    public void afterUpdate(BcSendPlatformModel model) {

    }

    @Override
    public void beforeDelete(BcSendPlatformModel model) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterDelete(BcSendPlatformModel model) {
        // TODO Auto-generated method stub

    }

}
